package com.zf.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        //测试
        System.out.println("双向链表的测试");
        HeroNode2 h1 = new HeroNode2(1,"宋江","及时雨");
        HeroNode2 h2 = new HeroNode2(2,"卢俊义","玉麒麟");
        HeroNode2 h3 = new HeroNode2(3,"吴用","智多星");
        HeroNode2 h4 = new HeroNode2(4,"林冲","豹子头");
        HeroNode2 h5 = new HeroNode2(4,"林冲2","豹子头222");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(h1);
        doubleLinkedList.add(h2);
        doubleLinkedList.add(h3);
        doubleLinkedList.add(h4);

        System.out.println("原始链表");
        doubleLinkedList.list();
        //修改
//        System.out.println("修改后的链表");
//        doubleLinkedList.update(h5);
//        doubleLinkedList.list();

        //删除
//        System.out.println("删除后的链表");
//        doubleLinkedList.del(3);
//        doubleLinkedList.list();

        //添加
        System.out.println("添加后的链表");
        doubleLinkedList.add(h5);
        doubleLinkedList.list();

    }

}

class DoubleLinkedList{
    //初始化头节点，不存放具体数据
    private HeroNode2 head = new HeroNode2(0,"", "");

    public HeroNode2 getHead() {
        return head;
    }

    public void add(HeroNode2 heroNode){

        HeroNode2 tmp = head;
        //遍历链表找到最后
        while (true){
            //找到最后
            if (tmp.next == null){
                break;
            }
            //未找到最后
            tmp = tmp.next;
        }

        //当退出循环时，tmp指向了链表的最后,将tmp指向新节点
        // 形成一个双向链表
        tmp.next = heroNode;
        heroNode.pre = tmp;

    }
    /**
     * 改
     * 根据no来修改
     * @param newHeroNode
     */
    public void update(HeroNode2 newHeroNode){
        if (head.next == null){
            System.out.println("链表为空");
        }
        HeroNode2 tmp = head.next;
        boolean flag = false;
        while (true){
            if (tmp == null){
                break;
            }
            if (tmp.no == newHeroNode.no){
                flag = true;
                break;
            }
            tmp = tmp.next;
        }

        if (flag){
            tmp.name = newHeroNode.name;
            tmp.nickname = newHeroNode.nickname;
        }else {
            System.out.printf("没有找到编号为%d的英雄",newHeroNode.no);
        }

    }

    /**
     * 删除节点
     * 1. 对于双向链表，我们可以直接找到要删除的节点
     * 2. 找到后，自我删除即可
     */
    public void del(int no){
        if (head == null){
            System.out.println("链表为空，无法删除！");
            return;
        }
        HeroNode2 tmp = head.next;
        boolean flag = false;

        while (true){
            if (tmp.next == null){
                break;
            }
            if (tmp.no == no){
                flag = true;
                break;
            }
            tmp = tmp.next;
        }
        if (flag){
            tmp.pre.next = tmp.next;

            //如果是最后一个节点就不需要执行下面的代码，否则会空指针异常
            if (tmp.next != null){
                tmp.next.pre = tmp.pre;
            }
        }else {
            System.out.printf("要删除的节点%d不存在\n",no);
        }
    }

    //遍历双向链表的方法
    public void list(){
        //判断节点是否为空
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }

        //利用辅助节点遍历链表
        HeroNode2 tmp = head.next;
        while (true){
            //判断链表是否到了最后
            if (tmp == null){
                break;
            }
            //输出节点的信息
            System.out.println(tmp);
            tmp = tmp.next;
        }
    }
}

class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;//指向下一个节点，默认为null
    public HeroNode2 pre;//指向前一个节点，默认为null

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }


}
